// pages/me/me.js
const app = getApp();
import { checkcertified, share } from '../../api/api';
Page({

  data: {
    height: app.globalData.height * 2 + 20, 
    imgUrl: app.globalData.imgUrl,
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '我的 ', //导航栏 中间的标题
      img: "../../image/index.png"
    },
    idx:true,
    navData:[
      {
         img: '/image/zl.png', 
         title: '完善个人信息',
         url: '/pages/me/userInfo/userInfo'
      },
      // { 
      //   img: '/image/xx.png',
      //    title: '我的消息', 
      //    url: '/pages/me/message/message' 
      // },
      {
         img: '/image/vip.png', 
         title: '我的会员卡', 
         url: '/pages/me/membership/membership'
      },
      { 
        img: '/image/dh.png',
        title: '已兑换商品', 
        url: '/pages/me/commodity/commodity' 
      },
    ],
    // 默认的用户信息
    username: '未登录',
    score: 0,
    avatar: '../../image/head.png',
    certifiedText: '未认证',
    show:false
  },

  bindViewTap: function (e) {
    if (!app.globalData.hasLogin || !app.globalData.hasMobile) {
      wx.navigateTo({ url: '/pages/me/login/login' })
    } else {
      const url = e.currentTarget.dataset.url;
      wx.navigateTo({ url })
    }
  },

  getUserInfo () {
    app.showLoading();
    app.setting()
      .then(res => {
        if (res.authSetting['scope.userInfo'] === true) {
          return app.getWxInfo();
        } else {
          // TODO
          // console.log('用户未授权')
        }
      })
      .then(res => {
        app.globalData.userInfo = res;
        this.setData({
          avatar: res.avatarUrl,
          text: '您好！欢迎进入福康汇'
        });
        return app.login();
      })
      .then(res => {
        return app.checkUser({ openid: res.openid });
      }).then(res => {
        if (res.code == 0 && res.data.mobile) {
          // console.log(res);
          app.globalData.hasMobile = true;
          app.globalData.userId = res.data.id;
          // res.data.avatar = app.globalData.imgUrl + res.data.avatar;
          app.globalData.userInfo = res.data;
          this.setData({
            score: parseInt(res.data.total_point) - parseInt(res.data.used_point),
            avatar: res.data.avatar,
            username: res.data.nickname,
          })
        }
        this.hideLoading();
      }).catch(err => {
        app.hideLoading();
      });
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.getUserInfo();
   this.checkcertified();
  },

  checkcertified () {
    const data = {
      openid: app.globalData.openid
    };
    const that = this;
    checkcertified(data).then(res => {
      if (res.code === 0) {
        that.setData({
          certifiedText: '已认证'
        })
      }
    })
  },
  /**
   * 点击显示分弹窗
   */
  checkShare(){
    if (this.data.show) {
      wx.showTabBar();
    } else {
      wx.hideTabBar();
    }
    this.setData({
      show:!this.data.show
    })
  },
  goRenzheng () {
    if (!app.globalData.hasLogin || !app.globalData.hasMobile) {
      wx.navigateTo({  url: '/pages/me/login/login' })
    }  else {
      if (app.globalData.userInfo.certified == 1) {
        return;
      } 
      wx.navigateTo({ url: '/pages/have/authentication/authentication' });
    }
  },

  onShow () {
    const user = app.globalData.userInfo || {};
    const arr = Object.keys(user);
    let score = 0;
    // console.log(user);
    if (arr.length !== 0) {
      if (user.total_point)  score = parseInt(user.total_point) - parseInt(user.used_point);
      let certifiedText = user.certified == 1 ? '已认证' : '未认证';
      this.setData({
        username: user.nickName || user.nickname,
        score,
        avatar: user.avatar,
        certifiedText
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    this.setData({ show: false });
    wx.showTabBar();
    // 分享获得积分
    const openid = app.globalData.openid;
    if (!openid) {
      return false;
    }
    share({ openid }).then(res => {
      if (res.code === 0 && app.globalData.userInfo.total_point) {
        app.globalData.userInfo.total_point = res.data.total_point;
        app.globalData.userInfo.used_point = res.data.used_point;
      }
    })
  },

  onHide () {
    if (this.data.show === true) {
      this.setData({ show: false })
    }
  }
})